//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package data_anno_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/data_anno_task_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type AnnoResultDbItem struct {
	ResultId      string `bson:"_id"`
	AnnoProjectId string `bson:"annoProjectId"`
	MemberUserId  string `bson:"memberUserId"`
	ResourceId    string `bson:"resourceId"`
	Result        string `bson:"result"`
}

func (item *AnnoResultDbItem) ToAnnoResult() *data_anno_task_api.AnnoResult {
	return &data_anno_task_api.AnnoResult{
		AnnoProjectId: item.AnnoProjectId,
		MemberUserId:  item.AnnoProjectId,
		ResourceId:    item.ResourceId,
		Result:        item.Result,
	}
}

type _AnnoResultDao struct {
	mc *mongo.Collection
}

func newAnnoResultDao(db *mongo.Database) (*_AnnoResultDao, error) {
	mc := db.Collection(db_ns.DB_NS_DATA_ANNO + "result")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"annoProjectId", 1}, {"memberUserId", 1}},
		},
		{
			Keys: bson.D{{"annoProjectId", 1}, {"resourceId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_AnnoResultDao{mc: mc}, nil
}

func (db *_AnnoResultDao) Insert(ctx context.Context, dbItem *AnnoResultDbItem) error {
	dbItem.ResultId = db.genResultId(dbItem.AnnoProjectId, dbItem.MemberUserId, dbItem.ResourceId)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_AnnoResultDao) genResultId(annoProjectId, memberUserId, resourceId string) string {
	return fmt.Sprintf("%s:%s:%s", annoProjectId, memberUserId, resourceId)
}

func (db *_AnnoResultDao) Exist(ctx context.Context, annoProjectId, memberUserId, resourceId string) (bool, error) {
	resultId := db.genResultId(annoProjectId, memberUserId, resourceId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": resultId, "annoProjectId": annoProjectId})
	return count > 0, err
}

func (db *_AnnoResultDao) Update(ctx context.Context, annoProjectId, memberUserId, resourceId, result string) error {
	resultId := db.genResultId(annoProjectId, memberUserId, resourceId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": resultId, "annoProjectId": annoProjectId}, bson.M{"$set": bson.M{"result": result}})
	return err
}

func (db *_AnnoResultDao) Get(ctx context.Context, annoProjectId, memberUserId, resourceId string) (*AnnoResultDbItem, error) {
	retItem := &AnnoResultDbItem{}
	resultId := db.genResultId(annoProjectId, memberUserId, resourceId)
	err := db.mc.FindOne(ctx, bson.M{"_id": resultId, "annoProjectId": annoProjectId}).Decode(retItem)
	return retItem, err
}

func (db *_AnnoResultDao) RemoveByMember(ctx context.Context, annoProjectId, memberUserId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"annoProjectId": annoProjectId, "memberUserId": memberUserId})
	return err
}

func (db *_AnnoResultDao) RemoveByResource(ctx context.Context, annoProjectId, resourceId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"annoProjectId": annoProjectId, "resourceId": resourceId})
	return err
}

func (db *_AnnoResultDao) RemoveByAnnoProject(ctx context.Context, annoProjectId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"annoProjectId": annoProjectId})
	return err
}
